<?php
/*
<!-- HTML FOR TESTING THE POST COMMAND -->
<html>
	<form action="getsongs.php" method="post">
	UserId: <input type="text" name="id" />
	<input type="submit" />Users
	</form>
</html>
*/
?>
<?php

	function getSong($id){
		$query = "SELECT * FROM  `Songs` WHERE `id`='".$id."'";
		$result = mysql_query($query);
		checkResult($result);
		$row = mysql_fetch_assoc($result);
		$song = array(
			"artist" => $row["Artist"],
			"title" => $row["Title"]
		);
		return $song;
	}

	function getName($id){
		$query = "SELECT * FROM  `Users` WHERE `id`='".$id."'";
		$result = mysql_query($query);
		checkResult($result);
		$row = mysql_fetch_assoc($result);
		return $row["Name"];
	}

	function checkResult($result){
		if(!$result){
	    		die($message);
		}
	}

	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	
		mysql_connect("localhost","akosciak","MojaPtaszek");
		
		mysql_select_db("songbump") or die( "Unable to select database");
	
		$id = $_POST["id"];
		$query = "SELECT * FROM  `SongsPlayed` WHERE `User`='".$id."' ORDER BY Time DESC LIMIT 10";
		$result = mysql_query($query);
		checkResult($result);
		
		// User Results
		$songs = array();
		while ($row = mysql_fetch_assoc($result)) {
			$songs[] = getSong($row["Song"]);
		}
		
		mysql_free_result($result);
			
		$songsJSON = json_encode($songs);
		echo $songsJSON;
		
	}
	
?>
